use hotel_reservation::grpc::reservation::reservation_service_client::ReservationServiceClient;
use hotel_reservation::grpc::reservation::{CreateReservationRequest, GetReservationRequest, UpdateReservationStatusRequest, ListReservationsRequest};
use hotel_reservation::grpc::reservation::ReservationStatus;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let args: Vec<String> = std::env::args().collect();
    
    if args.len() < 2 {
        println!("用法:");
        println!("  cargo run --bin simple_client -- create <name> <email> <phone> <room> <checkin> <checkout> <amount>");
        println!("  cargo run --bin simple_client -- get <id>");
        println!("  cargo run --bin simple_client -- list");
        println!("  cargo run --bin simple_client -- update <id> <status>");
        return Ok(());
    }

    let mut client = ReservationServiceClient::connect("http://[::1]:50051").await?;
    
    match args[1].as_str() {
        "create" => {
            if args.len() < 9 {
                println!("参数不足，需要: name email phone room checkin checkout amount");
                return Ok(());
            }
            
            let request = tonic::Request::new(CreateReservationRequest {
                guest_name: args[2].clone(),
                guest_email: args[3].clone(),
                guest_phone: args[4].clone(),
                room_number: args[5].clone(),
                check_in_date: args[6].clone(),
                check_out_date: args[7].clone(),
                total_amount: args[8].parse().unwrap_or(0.0),
            });

            let response = client.create_reservation(request).await?;
            println!("预订创建成功: {:?}", response.into_inner().reservation);
        }
        "get" => {
            if args.len() < 3 {
                println!("参数不足，需要: id");
                return Ok(());
            }
            
            let request = tonic::Request::new(GetReservationRequest {
                id: args[2].clone(),
            });

            let response = client.get_reservation(request).await?;
            println!("预订详情: {:?}", response.into_inner().reservation);
        }
        "list" => {
            let request = tonic::Request::new(ListReservationsRequest {
                page: 1,
                per_page: 10,
                guest_name: None,
                room_number: None,
                status: None,
            });
            let response = client.list_reservations(request).await?;
            println!("所有预订:");
            for reservation in response.into_inner().reservations {
                println!("  {:?}", reservation);
            }
        }
        "update" => {
            if args.len() < 4 {
                println!("参数不足，需要: id status");
                return Ok(());
            }
            
            let status = match args[3].as_str() {
                "pending" => ReservationStatus::Pending,
                "paid" => ReservationStatus::Paid,
                "checked_in" => ReservationStatus::CheckedIn,
                "checked_out" => ReservationStatus::CheckedOut,
                "cancelled" => ReservationStatus::Cancelled,
                _ => {
                    println!("无效的状态，使用: pending, paid, checked_in, checked_out, cancelled");
                    return Ok(());
                }
            };
            
            let request = tonic::Request::new(UpdateReservationStatusRequest {
                id: args[2].clone(),
                status: status as i32,
            });

            let response = client.update_reservation_status(request).await?;
            println!("状态更新成功: {:?}", response.into_inner().reservation);
        }
        _ => {
            println!("未知命令: {}", args[1]);
        }
    }
    
    Ok(())
}